package com.pearadmin.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pearadmin.common.nucleus.base.BaseController;
import com.pearadmin.common.nucleus.domain.response.Result;
import com.pearadmin.common.nucleus.domain.response.TableVO;
import com.pearadmin.common.nucleus.domain.response.TreeVO;
import com.pearadmin.system.domain.SysDept;
import com.pearadmin.system.service.ISysDeptService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("system/dept")
public class SysDeptController extends BaseController {

    private String MODULE_PATH = "system/dept/";

    @Resource
    private ISysDeptService sysDeptService;

    @GetMapping("main")
    public ModelAndView main(ModelAndView modelAndView){
        modelAndView.setViewName(MODULE_PATH+"main");
        return modelAndView;
    }

    @GetMapping("data")
    public TableVO data(){
        QueryWrapper queryWrapper = new QueryWrapper();
        List<SysDept> data = sysDeptService.list(queryWrapper);
        return TreeTable(data);
    }

    @GetMapping("add")
    public ModelAndView add(ModelAndView modelAndView){
        modelAndView.setViewName(MODULE_PATH+"add");
        return modelAndView;
    }

    @PostMapping("save")
    public Result save(@RequestBody SysDept sysDept){
        boolean result = sysDeptService.save(sysDept);
        return choice(result);
    }

    @DeleteMapping("deleteById/{id}")
    public Result deleteById(@PathVariable String id){
        boolean result = sysDeptService.removeById(id);
        return choice(result);
    }

    @GetMapping("edit")
    public ModelAndView edit(ModelAndView modelAndView,String id){

        SysDept sysDept = sysDeptService.getById(id);
        modelAndView.addObject("sysDept",sysDept);
        modelAndView.setViewName(MODULE_PATH+"edit");
        return modelAndView;
    }

    @PutMapping("update")
    public Result update(SysDept sysDept){
        boolean result = sysDeptService.updateById(sysDept);
        return choice(result);
    }

    @GetMapping("tree")
    public TreeVO tree(){
       List<SysDept> list = sysDeptService.list(null);
       return TreeData(list);
    }
}
